import streamlit as st
import plotly.express as px
from globals import FAVICON, APP_NAME, PROFILE_PAGE_NAME, PROFILE_KEY_BASE
from database.queries.user_orm import SynOrmUserQueries
from widgets.menu import menu_with_redirect
from widgets.profile_update import profile_update


st.set_page_config(
    page_title=f"{APP_NAME} • {PROFILE_PAGE_NAME}", 
    page_icon=FAVICON, 
    initial_sidebar_state="collapsed",
)
# Sidebar menu. Redirect if not authenticated
menu_with_redirect()

if "username" in st.session_state:
    user = SynOrmUserQueries.get_user_by_username(st.session_state["username"])
    update_tab, statistics_tab = st.tabs(["Обновление данных", "Статистика веса"])
    with update_tab:
        profile_update(PROFILE_KEY_BASE)
    with statistics_tab:
        dots = {
            "Время": [wu.time_stamp for wu in user.weight_updates],
            "Вес": [wu.weight for wu in user.weight_updates]
        }
        fig = px.area(dots, x="Время", y="Вес", line_shape="spline", markers=True)
        st.plotly_chart(fig)
